// pages/xishuo/details/details.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    li: [],
    all: false,
    show: true,
    value: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.setData({
      id: options.id ,
      my: options.my //判断是否是从用户发布页面进来的  隐藏关注按钮
    })

    this.list()

    // 适配iPhone X 底部 发表评论的
    if (wx.getSystemInfoSync().model.substr(0, 8) == "iPhone X") {
      this.setData({
        _top: true
      })
    }
  },
  list: function () {
    var _this = this;
    wx.ajax('/invatation/details/' + this.data.id).then(e => {
      _this.setData({
        item: e.data,
        li: e.data.pictures,
        likes: e.data.is_like
      })
    })
  },

  //回复评论
  hf: function (e) {



    // this.setData({
    //   value:'@'+e.currentTarget.dataset.name+':'
    // })

  },
  // 收藏
  shoucahng: function (e) {

    var _d = this.data.item
    _d.invitation_follow = !_d.invitation_follow
      this.setData({
        item: _d
      })
    wx.ajax('/invatation/follow/' + this.data.id, {}, 'get', true).then(res => {},e=>{
      _d.invitation_follow = !_d.invitation_follow
      this.setData({
        item: _d
      })
    })
  },
  all_show: function () {
    this.setData({
      all: true
    })
  },
  likes: function () {
    //  点赞
    var _this=this;
    var _dd=this.data.likes;
    var _li=this.data.item
    function add(){
      if(_dd || _dd =='1'){
        _li.support_cn.forEach(function(a,b){
          if(a.user_name==_this.data.user_name){
            _li.support_cn.splice(b,1)
          }
        })
        _dd=false
      }else
      {
        _dd=true
        _li.support_cn.push({user_avatar:_this.data.user_img,user_name:_this.data.user_name})
      }
      _this.setData({
        item:_li,
        likes:_dd
      })
    }
    if(wx.getStorageSync('uid')){
      add()
    }

    wx.ajax('/invatation/likes/' + this.data.id, {}, 'get', true).then(e => {},res=>{
      add()
    })




  },
  follow: function () {
    // 关注
    wx.ajax('/user/follow/' + this.data.item.user_id, {}, 'get', true).then(e => {
      this.list()
    })

  },
  img_list: function (e) {

    wx.previewImage({
      current: e.currentTarget.dataset.url, // 当前显示图片的http链接
      urls: this.data.li
    })


  },
  ln: function () {
    this.setData({
      show: true
    })
  },
  input: function (e) {
    this.setData({
      content: e.detail.value
    })
  },
  yes: function () {
    // 提交评论
    if (this.data.content == '' || !this.data.content) {
      wx.showToast({
        title: '评论内容不能为空',
        icon: 'none'
      })

    } else {
      wx.ajax('/invatation/comment', {
        id: this.data.id,
        content: this.data.content
      }, 'post', true).then(e => {

        wx.showToast({
          title: e.message,
        })
        this.setData({
          content: '',


        })
        this.list()
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
      var _this = this;
      if(wx.getStorageSync('uid')){
        wx.ajax('/user/index', {}, 'get', true).then(res => {
          _this.setData({
            user_img: res.user.user_avatar,
            user_name:res.user.name
          })
        })
      }
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.item.title,
      imageUrl: this.data.item.image,
      path: 'pages/xishuo/details/details?id=' + this.data.id
    }
  }
})